package com.auto.autoapp.task

import com.auto.autoapp.constants.Dy
import com.auto.autoapp.step.toDyIndex
import com.auto.autoapp.util.SizeUtil
import com.auto.autoapp.util.addLog
import com.auto.autoapp.util.clickOrTap
import com.auto.autoapp.util.getNodeById
import com.ven.assists.AssistsCore
import com.ven.assists.AssistsCore.getBoundsInScreen
import com.ven.assists.stepper.Step
import com.ven.assists.stepper.StepCollector
import com.ven.assists.stepper.StepImpl
import kotlinx.coroutines.delay


/**
 * 跳转抖音首页
 */
class ToDyIndex : StepImpl() {
    override fun onImpl(collector: StepCollector) {
        collector.next(1) {
            return@next toDyIndex()
        }
    }
}

/**
 * 获取抖音号
 */
class GetDyCodeTask : StepImpl() {

    override fun onImpl(collector: StepCollector) {
        collector.next(1) {
            return@next toDyIndex()
        }.next(2) {
            addLog("跳转首页成功，开始切换tab->我的")
            val result = Dy.ID_TAB_MINE.clickOrTap()
            if (result.isSuccess) {
                return@next Step.get(3)
            }
            return@next Step.get(1)
        }.next(3) {
            val nodeResult = Dy.ID_DY_CODE_TXT.getNodeById()
            if (nodeResult.isSuccess) {
                val node = nodeResult.getOrThrow()
                val codeInfo = node.text
                addLog("获取抖音号：$codeInfo")
            } else {
                addLog("获取抖音号失败，请检查用户是否已登陆")
            }

            return@next Step.none
        }
    }

}

/**
 * 切换抖音导航栏
 */
class ChangeIndexTab : StepImpl() {
    override fun onImpl(collector: StepCollector) {
        collector.next(1) {
            return@next toDyIndex()
        }.next(2) {
            val name = collector.get(1).data.toString()
            val topNavBox = AssistsCore.findById(Dy.INDEX_TOP_TAB).firstOrNull()
            var navRect = topNavBox?.getBoundsInScreen()
            if (navRect == null) {
                addLog("首页tab导航栏获取失败")
                return@next Step.none
            }

            var topBarNode =
                AssistsCore.findByTextAllMatch(name, filterViewId = Dy.INDEX_TOP_TAB_TEXT)
                    .firstOrNull()
            if (topBarNode == null) {
                addLog("导航栏查找\"${name}\"失败")
                return@next Step.none
            }
            var nodeRect = topBarNode.getBoundsInScreen()


            val innerResult = SizeUtil.horizontalOffsetToContain(nodeRect, navRect)
            if (!innerResult.first) {
                val offset = innerResult.second
                val startX = if (offset >= 0) navRect.left + 10f else navRect.right - 10f
                val startPosition = floatArrayOf(startX, navRect.centerY().toFloat())
                val endPosition = floatArrayOf(
                    startX + offset, navRect.centerY().toFloat()
                )
                val scrollResult = AssistsCore.gesture(startPosition, endPosition, 0, 100)
                if (!scrollResult) {
                    addLog("导航栏滑动失败")
                    return@next Step.none
                }
                addLog("滑动导航栏至$name")
                delay(1000)
                topBarNode =
                    AssistsCore.findByTextAllMatch(name, filterViewId = Dy.INDEX_TOP_TAB_TEXT)
                        .firstOrNull()
                if (topBarNode == null) {
                    addLog("滑动后未找到：$name")
                    return@next Step.none
                }


            }

            val result = topBarNode.clickOrTap()
            if (result.isSuccess) {
                addLog("切换至\"${name}\"")
            } else {
                addLog("切换失败,请重新尝试")
            }

            return@next Step.none
        }
    }
}

class getNodeTestTask : StepImpl() {
    override fun onImpl(collector: StepCollector) {

    }

}